﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using COM.Rights.Model;
using COM.Rights.BLL;
using System.Data;
using System.ServiceModel;
using System.ServiceModel.Activation;

/// <summary>
///WebService 的摘要说明

/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{

    public WebService()
    {

    }
    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public bool CheckField(string id, string name, string value)
    {
        if (Context.Request.Url.Host != Context.Request.UrlReferrer.Host)
            return false;
        if (!(string.IsNullOrEmpty(name) || name == "undefined"))
        {
            id = name.Substring(name.LastIndexOf("$") + 1);
        }

        id = id.Replace("Info_", "|");
        string[] temp = id.Split('|');
        if (temp.Length != 2)
            return false;
        string filter = temp[1] + "=@" + temp[1];
        List<DBParameter> dbs = new List<DBParameter>();
        dbs.Add(new DBParameter(temp[1], value));
        BaseBLL<BaseModel> bll = new BaseBLL<BaseModel>();
        switch (temp[0])
        {
            case "TRole":
                bll.ConfigName = "Rights"; break;
            case "TUser":
                bll.ConfigName = "Rights"; break;
            case "TFunction":
                bll.ConfigName = "Rights"; break;
            case "TOU":
                bll.ConfigName = "Rights"; break;
            case "TUserRightsIp":
                bll.ConfigName = "Rights"; break;

        }

        return bll.Exists(filter, temp[0], dbs);
    }

    [WebMethod]
    public List<TFunctionInfo> GetFunctions(string PGID)
    {
        TFunction bll = new TFunction();
        List<TFunctionInfo> infos = new List<TFunctionInfo>();
        string filter = "PGID=@PGID";
        List<DBParameter> dbs = new List<DBParameter>();
        dbs.Add(new DBParameter("PGID", PGID));
        infos = bll.GetList("", filter, "Sequence ASC", dbs);
        return infos;

    }

    [WebMethod]
    public bool DeleteTFunctionInfo(string GID, bool isAll)
    {
        TFunction bll = new TFunction();
        if (isAll)
        {
            bll.Remove(bll.GetChildren(GID));
            bll.Remove(GID);
            return true;
        }
        else
        {
            if (bll.Delete(GID) > 0)
                return true;
            else
                return false;
        }
    }

    [WebMethod]
    public bool AddTFunctionInfo(string GID, string PGID, string Code, string Name, string URI, int? FType, int? Sequence)
    {
        TFunction bll = new TFunction();
        TFunctionInfo info = InitTFunction(GID, PGID, Name, Code, URI, FType, Sequence);
        bll.Add(info, "");
        return true;
    }
    [WebMethod]
    public bool ModifyTFunctionInfo(string GID, string PGID, string Code, string Name, string URI, int? FType, int? Sequence)
    {
        TFunction bll = new TFunction();
        TFunctionInfo info = InitTFunction(GID, PGID, Name, Code, URI, FType, Sequence);
        info.SetIsNew(false);
        bll.Modify(info, "");
        return true;
    }

    public TFunctionInfo InitTFunction(string GID, string PGID, string name, string code, string uri, int? type, int? Sequence)
    {
        TFunctionInfo info = new TFunctionInfo();
        info.GID = GID;
        info.PGID = PGID;
        info.Name = name;
        info.Code = code;
        info.FType = type;
        info.URI = uri;
        info.Sequence = Sequence;
        return info;
    }

    [WebMethod]
    public bool AddTOUInfo(string GID, string PGID, string Name, string Code, string Manager, string OUType, int? Sequence)
    {
        TOU bll = new TOU();
        TOUInfo info = InitTOU(GID, PGID, Name, Code, Manager, OUType, Sequence);
        info.CreatTime = DateTime.Now;
        info.Creator = ZASession.ManagerUID;
        bll.Add(info, "");
        return true;
    }
    [WebMethod]
    public bool ModifyTOUInfo(string GID, string PGID, string Name, string Code, string Manager, string OUType, int? Sequence)
    {
        TOU bll = new TOU();
        TOUInfo info = InitTOU(GID, PGID, Name, Code, Manager, OUType, Sequence);
        info.SetIsNew(false);
        bll.Modify(info, "");
        return true;
    }

    public TOUInfo InitTOU(string GID, string PGID, string name, string code, string manager, string ouType, int? Sequence)
    {
        TOUInfo info = new TOUInfo();
        info.GID = GID;
        info.PGID = PGID;
        info.Name = name;
        info.Code = code;
        info.Manager = manager;
        info.OUType = ouType;
        info.Sequence = Sequence;
        return info;
    }

    [WebMethod]
    public bool DeleteTOUInfo(string GID, bool isAll)
    {
        TOU bll = new TOU();
        if (isAll)
        {
            bll.Remove(bll.GetChildren(GID));
            bll.Remove(GID);
            return true;
        }
        else
        {
            if (bll.Delete(GID) > 0)
                return true;
            else
                return false;
        }
    }

    [WebMethod]
    public string GetRolesByOU(string ouGID)
    {
        TRoleTTTOU bll = new TRoleTTTOU();
        return bll.GetRoleIDSByOU(ouGID);
    }

    [WebMethod]
    public bool SaveOURoles(string roleIDS, string ouGID)
    {
        TRoleTTTOU bll = new TRoleTTTOU();
        bll.SaveRols(ouGID, roleIDS);
        return true;
    }
    [WebMethod]
    public string GetJsonRowByID(string tableName, string id, string idField, string fields)
    {
        DataView view = ZData.GetTable(tableName).AsDataView();
        view.RowFilter = idField + "='" + id + "'";
        string result = "";
        if (view.Count > 0)
        {
            result += "{ ";
            foreach (string field in fields.Split(','))
            {
                if (string.IsNullOrEmpty(field))
                    continue;
                result += "\"" + field + "\":\"" + view[0][field].ToString() + "\",";
            }
            result = result.Trim(',');
            result += "}";
        }
        return result;
    }
}
